<template> 
    <div class="site-list">
        <r-site type="group" @radioChange="radioChange" @deleteItem="deleteItem" @select="selectItem" v-for="(item,index) in siteDataList" :item="item" :key="index"></r-site> 

        <div class="footer">
            <div class="btn text-center" @click="addSite">新增地址</div>
        </div>
    </div> 
</template>

<script> 
import * as api from '@/utils/api' 
import rSite from '@/components/tpl/site' 
import store from './../recipe-pay/store' 
export default {
    onShow(){
        this.getSiteList();
    },
    components: { 
        rSite
    }, 
    data () {
        return {
            siteDataList:[]
        }
    },
    methods:{
        //地址列表
        getSiteList(){
            let vm = this;
            let body={
                model: "Address",
                condition:{
                    uid: wx.getStorageSync('u_id'),
                    is_delete:'0'
                }
            };
            api.commonSite(body).then(res=>{
                if(res.data.code==200){
                    let data=res.data.data.data;
                    let arr=[];
                    data.forEach((item,index)=>{
                        item.is_default==0?item.checked=false:item.checked=true; 
                        arr.push(item);
                    }); 
                    vm.siteDataList=arr;
                }
            });
        },
        //设为默认地址
        radioChange(item){ 
            let vm = this;
            let body={
                model: "Address",
                data:{
                    is_default:item.checked?1:0
                },
                update_condition:{ 
                    id:item.id, 
                }
            };
            api.createSite(body).then(res=>{
                if(res.data.code==200){
                    vm.getSiteList();
                }
            });
        },
        //选择的地址
        selectItem(item){ 
            let vm =this;
            let body={
                model: "Orders",
                data: { 
                    address_id:item.id 
                },
                update_condition:{
                    id: store.state.orderNo 
                }

            };
            api.createSite(body).then(res=>{
                if(res.data.code==200){ 
                    store.state.paySiteList=item;
                    wx.navigateBack({ delta: 1  });
                }
            })
            
        },
        //删除
        deleteItem(item){
            let vm =this;
            let body={
                model: "Address",
                data: { 
                    is_delete:'1' 
                },
                update_condition:{
                    id: item.id
                } 
            };
            api.createSite(body).then(res=>{
                if(res.data.code==200){  
                    this.getSiteList();
                }
            })
        },
        //新增地址
        addSite(){
            let url='./../new-site/main';
            wx.navigateTo({url});
        },
    } 
}

</script>
 
<style scoped lang="scss">
.site-list{
    padding:20rpx;
    box-sizing:border-box;
    .footer{
        width:100%;
        margin-top:40rpx;
        font-size:30rpx;
        .btn{
            width:80%;
            padding:20rpx;
            box-sizing:border-box;
            margin:auto;
            background:#CD4640;
            color:#fff;
            border-radius:20rpx;
            &:active{
                background-color:rgba(205,70,64,0.5)
            }
        }
    }
} 
</style>
